package fr.upmc.tps.mdoc.servlets;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessionBeans.GestionContactRemote;
import fr.upmc.tps.mdoc.domain.DAOContact;
import fr.upmc.tps.mdoc.domain.DAOContactGroup;
import fr.upmc.tps.mdoc.domain.IDAOContact;
import fr.upmc.tps.mdoc.domain.IDAOContactGroup;
import fr.upmc.tps.mdoc.models.Address;
import fr.upmc.tps.mdoc.models.Contact;
import fr.upmc.tps.mdoc.models.Entreprise;
import fr.upmc.tps.mdoc.models.IContact;
import fr.upmc.tps.mdoc.models.IContactGroup;
import fr.upmc.tps.mdoc.models.PhoneNumber;

/**
 * Servlet implementation class NewContact
 */
public class NewContact extends HttpServlet {
	private static final long serialVersionUID = 1L;

	// Utilisation des EJBs
	
	GestionContactRemote beanRemote;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NewContact() {
		super();
	}

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.getRequestDispatcher("pages/addContact.jsp").forward(request,
				response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		IDAOContact dao = new DAOContact();

		String firstName = request.getParameter("firstName");

		String lastName = request.getParameter("lastName");
		String email = request.getParameter("email");

		// Si la case Entreprise est coché, il faut créer une entreprise
		// et non un simple contact.
		IContact contact;

		if (!request.getParameter("numSiret").isEmpty()) {

			System.out.println("Entreprise value: "
					+ request.getParameter("numSiret"));

			long numSiret = Long.parseLong(request.getParameter("numSiret"));
			contact = new Entreprise(firstName, lastName, email, numSiret);

		} else {

			contact = new Contact(firstName, lastName, email);

		}

		// L'adresse du contact
		Address adr = new Address();
		adr.setStreet(request.getParameter("street"));
		adr.setCity(request.getParameter("city"));
		adr.setCountry(request.getParameter("country"));
		adr.setZip(request.getParameter("zip"));

		contact.setAddress(adr);

		// Les téléphones du contact
		contact.getPhones().add(
				new PhoneNumber("fixe", request.getParameter("phoneFixe"),
						contact));
		contact.getPhones().add(
				new PhoneNumber("port", request.getParameter("phonePort"),
						contact));
		contact.getPhones().add(
				new PhoneNumber("bureau", request.getParameter("phoneOffice"),
						contact));

		// Récupération de cases cochés par l'utilisateur
		String groupnames[] = request.getParameterValues("groupname");

		IDAOContactGroup daog = new DAOContactGroup();
		List<IContactGroup> groups = daog.getGroups();
		// ContactGroup gr = new ContactGroup();
		Set<IContactGroup> scg = new HashSet<IContactGroup>();
		for (String g : Arrays.asList(groupnames))

			for (IContactGroup cg : groups) {

				if (Integer.parseInt(g) == cg.getGroupId()) {
					// gr = daog.loadGroup(cg.getGroupId());
					// cg.setGroupName(g);
					scg.add(cg);
				}

				// contact.getBooks().add(cg);//
				// request.getParameter("groupName")));

			}

		contact.setBooks(scg);
		// FIN groupe

		dao.addContact(contact);

		request.getRequestDispatcher("pages/accueil.jsp").forward(request,
				response);
	}
}
